const router = require('koa-router')();
const koa2Req = require('koa2-request');
const {
  addMemberService,
  getUserOpenIdService
} = require('../../service/memberService');

//新增会员信息
router.post("/api/miniapps/addMemberInfo", async (ctx, next) => {
  let params = ctx.request.body;
  await addMemberService(ctx, params);
})

//获取用户openid
router.get("/api/miniapps/getUserOpenId", async (ctx, next) => {
  let params = ctx.request.query;
  const appId = 'wxbe873efd6206a0cd'; // AppID(小程序ID)wx255ec178c1a6de9d
  const appSecret = '32d9dfa4e9d77cf6c1ce5adaea9af314';// AppSecret(小程序密钥)
  try {
    const res = await koa2Req({
      url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${appSecret}&js_code=${params.code}&grant_type=authorization_code`
    })
    let data = JSON.parse(res.body);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {
    ctx.body = {
      "success": false,
      "message": error
    };
  }
})
//获取openid的接口
router.get("/api/miniapps/getUserOpenIdNew", async (ctx, next) => {
  const appId = 'wxbe873efd6206a0cd'; // AppID(小程序ID)wx255ec178c1a6de9d
  const appSecret = '32d9dfa4e9d77cf6c1ce5adaea9af314';// AppSecret(小程序密钥)
  let params = ctx.request.query;
  try {
    const res = await koa2Req({
      url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${appSecret}&js_code=${params.code}&grant_type=authorization_code`
    })
    let data = JSON.parse(res.body);
    ctx.body = {
      "success": true,
      "data": data
    }
  } catch (err) {
    ctx.body = {
      "success": false,
      "data": err
    }
  }
})

module.exports = router;


